#!/bin/bash
#source config.sh

export 'PS4=[`date +"%m-%d"`]`basename $0`.$LINENO+ '

added_key="[dgggit09-rd.huawei.com]:29418,[10.1.21.40]:29418 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAxhN+TnUC8a91hpMJmuYMOl+X7S2XlyGhhIQEBvZTcjqF/FXN2u9Jn4SkeXsAi6J+P1q3nFKbgqQKpug2f41Q6xufXn4wsnACcjVAzVmSSe+qvMUh84oiI7j7c7MZ9iQhxgMzrJIMoP20wsVjEyQtT2jlrxb8+h6dzq+sk2HhX4wA+rsIOFrSQvKdIjrphCoVJ+vadX+dtk5BD+Etdu22XlPl3AfZONpssPmEc/hM96u44NtHE+BujDhBhXYW8VKYKW1ZiQ0x/27YR2jss572jlEegOCMq/GmoiRwWM7Y7ZBZ8rrEg85lpjIDKCfh8r1Hiz6wrrWfQGdSHukUEL2aqQ=="

if cat /root/.ssh/known_hosts | grep "dgggit09-rd.huawei.com" > /dev/null;then
    echo "Key is OK"
else
    echo "$added_key" >> /root/.ssh/known_hosts
fi
# 新机器需要添加pub-key 
rm -f /root/.ssh/id_rsa /root/.ssh/id_rsa.pub

cat >/root/.ssh/id_rsa.pub <<L_EOF
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDU/h5m79Voc4Kzyf9bNardbQYuSicnOc4ERv5tAtZOgAZwV9s4M4J7++ZxugFPUcbJs9nIHwlhBNVSxWcgnp7DlfEzysuv72dIwrEobdEUiIpKv9GuHNWvJRM0ltRVIlO1P4iUAwqPXaCCMOEARKL3qVNVmuZM1UcgGOLBxq2V/mqJdf8S1vYTu7lOxwhSHSJLxzhaYNViOKiPrjMaVRkieOLLLiqjZE4pgXgM3bl+2huR9Gamo2ZWTCLJ2Y+Ueek4tfHMvzTurXFYEVCjBJfd6YpJ55wh6HqiXsd5biyM2bNECGSDnRGk52qLh43RR+BLxB1soCpoULP/T52G6QSX eulerosci@huawei.com
L_EOF

cat << END1 > /root/.ssh/id_rsa
-----BEGIN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sm+udMvg7NSkp4TECgYEA0dAn04ZIS7B+qGEXLUZW
qazYOJ5PELnOg7kGnTVszZpQVGBWK+xEIIgVV3SFpN8DW2bcxZaHja0Zo2IBrViR
S/pQFrw7/hN4BRdcrT1Y/VWeejJrmZlmR6Lfo5Ng2IGBOUgI2tom/Arre3AXsGEz
TjbVufvgv/5hpruW52urA4MCgYEArc4bq9zPWGAsFSzYK0aC2j3vvkllhvFf3ZJr
KyxWrtRbtezzhY/oRbEmayjPQS5eabTL5bqyHxYSEzndBHw0FpBvr8aoOiiXfr8v
FYyY1Ektlt0CMCBsBE/kRkwrQwrPX+d+q3PyJI64WMwM7Ow+E7srqAqclkNBx8IS
6x3kRPECgYBRCR23MOCKJ0wbdJaOyPVwYOT/2Roz+vcsb4ufVRVq4X2rACq7aaVg
kW5Nkoovsw/UyY5lg8RalWPxKJ/vQ2pMwhbKZNVFgzBLJv+esjlUGrl55KatFrGI
ZAIAY2MnglnEhNvkV7UDJfFJoKzgVGnvSsqyICeGKLNtZz9wAS+bug==
-----END RSA PRIVATE KEY-----
END1

chmod 644 /root/.ssh/id_rsa.pub
chmod 600 /root/.ssh/id_rsa

rm -f ./msg.log
ssh -p 29418 peulerosci@dgggit09-rd.huawei.com gerrit query change:$GERRIT_CHANGE_NUMBER > ./msg.log
if ! cat ./msg.log | grep commit &> /dev/null;then
    exit 1
fi

python msg_check.py
